﻿using ManagementApi.Models;
using ManagementApi.Models.User;
using ManagementApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Corp;
using Modules.Project;
using Modules.System.User;
using Modules;
using PmSoft.ApplicationContext;
using ManagementApi.Models.System.User;
using ManagementApi.Models.Person;
using AutoMapper;
using Modules.System.Role;

namespace ManagementApi.Controllers
{
    /// <summary>
    /// 微信相关接口
    /// </summary>
    [ApiController]
    [Route("/api/v1/weixin")]
    public class WeiXinController : ControllerBase
    {
        private readonly WeiXinService weiXinService;
        private readonly IApplicationContext applicationContext;
        private readonly UserService userService;
        private readonly IMapper mapper;
        private readonly RoleService roleService;

        public WeiXinController(WeiXinService weiXinService, IApplicationContext applicationContext, UserService userService, IMapper mapper, RoleService roleService)
        {
            this.weiXinService = weiXinService;
            this.applicationContext = applicationContext;
            this.userService = userService;
            this.mapper = mapper;
            this.roleService = roleService;
        }

        /// <summary>
        /// 获取token
        /// </summary>
        /// <returns></returns>
        [HttpGet("getToken")]
        public async Task<ApiResult<string>> GetToken()
        {
            var token = await weiXinService.GetAccessToken();
            return ApiResult<string>.Success(token);
        }

        /// <summary>
        /// 微信登录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("login")]
        public async Task<ApiResult<string>> GetLogin(string code)
        {
            string openId = await weiXinService.GetLogin(code);
            return ApiResult<string>.Success(openId);
        }
    }
}